<template>
	<div class="detail_main">
		<div class="detail_main_top">
			<el-button type="primary" @click="$router.back(-1)" size='small' class='detail_top_leftP1'>返回</el-button>
			<span>{{ caseinfo.caseName }}（影像报告）</span>
			<div class="detail_top_right">
				<el-button type='primary' size='small' @click='showcase = true'>病例病史</el-button>
				<el-button type='primary' size='small' @click='toCase'>查看影像</el-button>
				<el-button type='primary' size='small' @click='toCaseDetail'>诊断导图</el-button>
			</div>
		</div>
		<div class="detail_main_bottom clearfix">
			<div class="csae_dio_contboxin">
				<div class="csae_dio_contitem all">
					<div class="csae_dio_title">
						正确报告 - 诊断报告单
					</div>
					<div class="csae_dio_main all beautifulScroll">
						<el-row class='csae_dio_mainP1'>
							<el-col :span='6'>
								<div class="csae_dio_mainP2">
									<b>姓名：</b>{{ caseinfo.patientName }}
								</div>
							</el-col>
							<el-col :span='6'>
								<div class="csae_dio_mainP2">
									<b>年龄：</b>{{ caseinfo.patientAge }}岁
								</div>
							</el-col>
							<el-col :span='12'>
								<div class="csae_dio_mainP2">
									<b>性别：</b>{{ caseinfo.patientSex == "F" ? '女' : '男' }}
								</div>
							</el-col>
							<el-col :span='6'>
								<div class="csae_dio_mainP2">
									<b>病历号：</b>{{ caseinfo.number }}
								</div>
							</el-col>
							<el-col :span='6'>
								<div class="csae_dio_mainP2">
									<b>检查类型：</b><span v-for='item in modalitys'
										v-if='caseinfo.modalityId == item.modalityId'>{{ item.modalityName }}</span>
								</div>
							</el-col>
							<el-col :span='12'>
								<div class="csae_dio_mainP2">
									<b>检查时间：</b>{{ caseinfo.studyTime }}
								</div>
							</el-col>
						</el-row>
						<el-row class='csae_dio_mainP1'>
							<el-col :span='24'>
								<div class="csae_dio_mainP2">
									<b>检查部位：</b>{{ caseinfo.bodyName }}
								</div>
							</el-col>
						</el-row>
						<el-row class='csae_dio_mainP1 bottom'>
							<el-col :span='24'>
								<div class="csae_dio_mainP2">
									<b>影像表现：</b>
								</div>
								<div class="csae_dio_mainP3">
									{{ caseinfo.find }}
								</div>
							</el-col>
							<el-col :span='24'>
								<div class="csae_dio_mainP2">
									<b>检查结论：</b>
								</div>
								<div class="csae_dio_mainP3">
									{{ caseinfo.diagnosis }}
								</div>
							</el-col>
						</el-row>
					</div>
				</div>
				<div class="csae_dio_contitem all">
					<div class="csae_dio_title">
						我的-诊断报告单
					</div>
					<div class="csae_dio_contcontrols">
						<div class="csae_dio_contcontrol " @click="changeNum(-1)"
							:class="reportIndex <= 0 ? 'disabled' : ''">
							<i class="el-icon-arrow-left"></i>
						</div>
						<div class="l csae_dio_contcontrol_num">
							{{ reportIndex + 1 }}/{{ reports.length }}
						</div>
						<div class="csae_dio_contcontrol " @click="changeNum(1)"
							:class="reportIndex >= reports.length - 1 ? 'disabled' : ''">
							<i class="el-icon-arrow-right"></i>
						</div>
					</div>
					<emptymain :emptytext="'暂无诊断报告'" :emptyheight="'550px'" :emptylist='reports'>
						<span></span>
					</emptymain>
					<div class="csae_dio_main noborder all beautifulScroll" v-if="reportIndex > -1">
						<div class="csae_dio_item">
							<el-row class='csae_dio_mainP1'>
								<el-col :span='6'>
									<div class="csae_dio_mainP2">
										<b>姓名：</b>{{ caseinfo.patientName }}
									</div>
								</el-col>
								<el-col :span='6'>
									<div class="csae_dio_mainP2">
										<b>年龄：</b>{{ caseinfo.patientAge }}岁
									</div>
								</el-col>
								<el-col :span='12'>
									<div class="csae_dio_mainP2">
										<b>性别：</b>{{ caseinfo.patientSex == "F" ? '女' : '男' }}
									</div>
								</el-col>
								<el-col :span='6'>
									<div class="csae_dio_mainP2">
										<b>病历号：</b>{{ caseinfo.number }}
									</div>
								</el-col>
								<el-col :span='6'>
									<div class="csae_dio_mainP2">
										<b>检查类型：</b><span v-for='item in modalitys'
											v-if='caseinfo.modalityId == item.modalityId'>{{ item.modalityName }}</span>
									</div>
								</el-col>
								<el-col :span='12'>
									<div class="csae_dio_mainP2">
										<b>提交时间：</b>{{ reports[reportIndex].createTime }}
									</div>
								</el-col>
							</el-row>
							<el-row class='csae_dio_mainP1'>
								<el-col :span='24'>
									<div class="csae_dio_mainP2">
										<b>检查部位：</b>{{ caseinfo.bodyName }}
									</div>
								</el-col>
							</el-row>
							<el-row class='csae_dio_mainP1 bottom'>
								<el-col :span='24'>
									<div class="csae_dio_mainP2">
										<b>影像表现：</b>
									</div>
									<div class="csae_dio_mainP3">
										{{ reports[reportIndex].find }}
									</div>
								</el-col>
								<el-col :span='24'>
									<div class="csae_dio_mainP2">
										<b>检查结论：</b>
									</div>
									<div class="csae_dio_mainP3">
										{{ reports[reportIndex].diagnosis }}
									</div>
								</el-col>
							</el-row>
						</div>
						<div class="csae_dio_teacher" v-if="reports[reportIndex].score">
							<div class="csae_dio_teacherP1">评分评语</div>
							<div class="csae_dio_teacherP2">
								<el-rate v-model="reports[reportIndex].score / 2" :colors="colors" show-text
									:texts='texts' disabled>
								</el-rate>
							</div>
							<div class="csae_dio_teacherP2">
								<div class="csae_dio_mainP2 big">
									<b>教师评语：</b>
								</div>
								<div class="csae_dio_mainP3 big">
									<span class="editor_box" v-html="reports[reportIndex].evaluate"></span>
								</div>
							</div>
						</div>
					</div>
					<div class="csae_dio_bottom" v-if="reportIndex > -1">
						<el-button type='success' :loading="aiLoading"
							@click='aiItemReport(reports[reportIndex])'>报告分析</el-button>
						<el-button type='danger' @click='deleteItemReport(reports[reportIndex])'>删除报告</el-button>
					</div>
				</div>
			</div>
		</div>
		<div class="index_mask" v-show='showcase'>
			<div class="csae_dio_cont">
				<div class="csae_dio_title">
					患者及病史信息
				</div>
				<div class="csae_dio_close like_btn" @click="showcase = false">
					<i class="el-icon-close"></i>
				</div>
				<div class="csae_dio_main beautifulScroll">
					<el-row class='csae_dio_mainP1'>
						<el-col :span='8'>
							<div class="csae_dio_mainP2">
								<b>姓名：</b>{{ caseinfo.patientName }}
							</div>
						</el-col>
						<el-col :span='8'>
							<div class="csae_dio_mainP2">
								<b>年龄：</b>{{ caseinfo.patientAge }}岁
							</div>
						</el-col>
						<el-col :span='8'>
							<div class="csae_dio_mainP2">
								<b>性别：</b>{{ caseinfo.patientSex == "F" ? '女' : '男' }}
							</div>
						</el-col>
						<el-col :span='8'>
							<div class="csae_dio_mainP2">
								<b>病历号：</b>{{ caseinfo.number }}
							</div>
						</el-col>
						<el-col :span='8'>
							<div class="csae_dio_mainP2">
								<b>检查类型：</b><span v-for='item in modalitys'
									v-if='caseinfo.modalityId == item.modalityId'>{{ item.modalityName }}</span>
							</div>
						</el-col>
					</el-row>
					<el-row class='csae_dio_mainP1'>
						<el-col :span='24'>
							<div class="csae_dio_mainP2">
								<b>主诉：</b>{{ caseinfo.number }}
							</div>
						</el-col>
					</el-row>
					<el-row class='csae_dio_mainP1'>
						<el-col :span='24'>
							<div class="csae_dio_mainP2">
								<b>既往史：</b>{{ medicalHistory0.value || '无' }}
							</div>
						</el-col>
						<el-col :span='24'>
							<div class="csae_dio_mainP2">
								<b>家族史：</b>{{ medicalHistory1.value || '无' }}
							</div>
						</el-col>
						<el-col :span='24'>
							<div class="csae_dio_mainP2">
								<b>婚育史：</b>{{ medicalHistory2.value || '无' }}
							</div>
						</el-col>
						<el-col :span='24'>
							<div class="csae_dio_mainP2">
								<b>现病史：</b>{{ medicalHistory3.value || '无' }}
							</div>
						</el-col>
						<el-col :span='24'>
							<div class="csae_dio_mainP2">
								<b>个人史：</b>{{ medicalHistory4.value || '无' }}
							</div>
						</el-col>
					</el-row>
					<el-row class='csae_dio_mainP1 bottom'>
						<el-col :span='24'>
							<div class="csae_dio_mainP2">
								<b>附件：</b> {{ !caseinfo.application && !caseinfo.hospitalCase ? '无' : '' }}
								<el-button type='primary' size='small' @click="openImg('application')"
									v-if='caseinfo.application'>检查申请单</el-button>
								<el-button type='primary' size='small' @click="openImg('hospitalCase')"
									v-if='caseinfo.hospitalCase'>住院病例</el-button>
							</div>
						</el-col>
					</el-row>
				</div>
			</div>
		</div>
		<div class="index_mask" v-show='caseType'>
			<div class="csae_dio_cont auto">
				<div class="csae_dio_title">
					{{ caseType == 'application' ? '检查申请单' : '住院病例' }}
				</div>
				<div class="csae_dio_close like_btn" @click="closeImg">
					<i class="el-icon-close"></i>
				</div>
				<div class="csae_dio_main nobar">
					<img :src="baseurl + caseImg" class="csae_dio_mainimg" />
				</div>
			</div>
		</div>
		<reportAnalysismain :reportAnalysis="reports[reportIndex].reportAnalysis" :infoForm="caseinfo"
			:reportDataInfo="reports[reportIndex]" :modalitys="modalitys"
			v-if="reportAnalysisShow" @closeOpen="reportAnalysisShow = false">
		</reportAnalysismain>
	</div>
</template>

<script>
import { caseInfo, reportList, deleteReport, aiReportAnalysis } from '@/api/case.js'
import { modalityList } from '@/api/image.js'
import mindmap from '@/views/case/mindmap.vue'
import detailright from '@/views/case/detailright.vue'
import reportAnalysismain from '@/views/case/reportAnalysis.vue'
export default {
	components: {
		mindmap,
		detailright,
		reportAnalysismain
	},
	data() {
		return {
			caseId: this.$route.query.caseId,
			reportId: this.$route.query.reportId,
			colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
			texts: ['2分  失望', '4分  不满', '6分  一般', '8分  满意', '10分  惊喜'],
			showcase: false,
			aiLoading: false,
			reportAnalysisShow: false,
			modalitys: [],
			medicalHistory0: {
				label: '既往史',
				value: ''
			},
			medicalHistory1: {
				label: '家族史',
				value: ''
			},
			medicalHistory2: {
				label: '婚育史',
				value: ''
			},
			medicalHistory3: {
				label: '现病史',
				value: ''
			},
			medicalHistory4: {
				label: '个人史',
				value: ''
			},
			caseinfo: {},
			caseImg: '',
			caseType: '',

			reports: [],
			reportIndex: -1,

		}
	},
	created() {
		this.getDataInfo()
		this.getModalityList()
		this.getDataList()
	},
	mounted() {

	},
	methods: {
		aiItemReport(item) {
			if (this.reports[this.reportIndex].reportAnalysis) {
				this.reportAnalysisShow = true
				return
			}
			var patientSex = this.caseinfo.patientSex == "F" ? '女' : '男';
			var patientCase = '性别：' + patientSex + '，';
			patientCase += '年龄：' + this.caseinfo.patientAge + '岁;';
			var list = [0, 1, 2, 3, 4]
			list.map(item => {
				patientCase += this['medicalHistory' + item].label + ':' + this['medicalHistory' + item].value + '；'
			})
			var data = {
				patientCase,
				systemReport: '影像所见：' + this.caseinfo.find + '；影像结论：' + this.caseinfo.diagnosis,
				studentReport: '影像所见：' + item.find + '；影像结论：' + item.diagnosis,
			}
			this.aiLoading = true
			aiReportAnalysis(data).then(res => {
				if (res.code == 200) {
					this.reports[this.reportIndex].reportAnalysis = res.data
					this.reportAnalysisShow = true
				}
			}).finally(() => {
				this.aiLoading = false
			})
		},
		closeImg() {
			this.caseImg = '';
			this.caseType = '';
		},
		openImg(type) {
			this.caseType = type;
			this.caseImg = this.caseinfo[type];
		},
		toCase() {
			const routeUrl = this.$router.resolve({
				name: 'caseimage',
				query: {
					caseId: this.caseId,
				}
			})
			window.open(routeUrl.href, '_blank')
		},
		toCaseDetail() {
			this.$router.push({
				name: 'casedetail',
				query: {
					caseId: this.caseId,
				}
			})
		},
		changeNum(num) {
			if (num < 0 && this.reportIndex > 0) {
				this.reportIndex--
			}
			if (num > 0 && this.reportIndex < this.reports.length - 1) {
				this.reportIndex++
			}
		},
		getModalityList() {
			modalityList({}).then(res => {
				this.modalitys = res.data || [];
			})
		},
		getDataInfo() {
			caseInfo({
				caseId: this.caseId,
			}).then(res => {
				this.caseinfo = res.data || {};
				if (this.caseinfo.medicalHistory) {
					var medicalHistory = JSON.parse(this.caseinfo.medicalHistory);
					medicalHistory.map(item => {
						this[item.name] = item
					})
				}
			})
		},
		deleteItemReport(item) {
			this.$confirm('确认删除此诊断报告吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				deleteReport({
					reportId: item.reportId,
				}).then(res => {
					if (res.code == 200) {
						this.getDataList()
					} else {
						this.$message({
							type: 'error',
							message: res.message
						})
					}
				})
			}).catch(() => { })
		},
		getDataList() {
			reportList({
				caseId: this.caseId,
			}).then(res => {
				this.reports = res.data || [];
				if (this.reports && this.reports.length) {
					if (this.reportId) {
						this.reports.map((item, index) => {
							if (item.reportId == this.reportId) {
								this.reportIndex = index
							}
						})
					} else {
						this.reportIndex = 0
					}
				} else {
					this.reportIndex = -1
				}
			})
		},
	},
}
</script>